

local Event = commonlib.inherit(commonlib.ToolBase);


Event:Property("EventType");
Event:Property("Window");
Event:Property("Accept", false, "IsAccept", "SetAccept");
Event:Property("LeftButton", false, "IsLeftButton");
Event:Property("RightButton", false, "IsRightButton");
Event:Property("LeftButtonPressed", false, "IsLeftButtonPressed");
Event:Property("RightButtonPressed", false, "IsRightButtonPressed");

local LEFT_MOUSE_BUTTON = 1;
local RIGHT_MOUSE_BUTTON = 2;
local MIDDLE_MOUSE_BUTTON = 3;

function Event:ctor()
    self.quick_click_count = 0;                        -- 快速左击次数
    self.last_click_timestamp = 0;                     -- 最近一次点击事件戳 
    self.last_mouse_x, self.last_mouse_y = 0, 0;       -- 最近一次前鼠标坐标
    self.mouse_x, self.mouse_y = 0, 0;                 -- 当前鼠标坐标
    self.mouse_button = 0;                             -- 鼠标按键值 
    self.mouse_wheel_x = 0;                            -- 水平滚动
    self.mouse_wheel_y = 0;                            -- 垂直滚动

    self.window_x, self.window_y = 0, 0;               -- 鼠标窗口坐标

    self.keycode = 0;                                  -- 按键码
    self.char = 0;                                     -- 输入字符
end

function Event:Init(window, event_type)
    self:SetWindow(window);
    self:SetEventType(event_type);
    self:SetAccept(false);
    return self;
end

function Event:Accept()
    self:SetAccept(true);
end

function Event:InitMouseEvent(window, mouse_event_type, mouse_x, mouse_y, mouse_button, mouse_wheel_x, mouse_wheel_y)
    self:Init(window, mouse_event_type);
    
    self:SetLeftButton(mouse_button == LEFT_MOUSE_BUTTON);
    self:SetRightButton(mouse_button == RIGHT_MOUSE_BUTTON);

    mouse_x, mouse_y = mouse_x or self.last_mouse_x, mouse_y or self.last_mouse_y;

    if (mouse_event_type == "onmousedown") then
        if (mouse_button == LEFT_MOUSE_BUTTON) then self:SetLeftButtonPressed(true) end
        if (mouse_button == RIGHT_MOUSE_BUTTON) then self:SetRightButtonPressed(true) end
        local cur_ts = commonlib.GetTimeStamp();
        -- 是否需要位置坐标一致
        if (self:IsLeftButtonPressed()) then
            if ((cur_ts - self.last_click_timestamp) < 250) then 
                self.quick_click_count = self.quick_click_count + 1;
            else
                self.quick_click_count = 1;
            end
            self.last_click_timestamp = cur_ts;
        end
    end
    
    if (mouse_event_type == "onmouseup") then
        if (mouse_button == LEFT_MOUSE_BUTTON) then self:SetLeftButtonPressed(false) end
        if (mouse_button == RIGHT_MOUSE_BUTTON) then self:SetRightButtonPressed(false) end
    end

    self.last_mouse_x, self.last_mouse_y = self.mouse_x, self.mouse_y;
    self.mouse_x, self.mouse_y, self.mouse_button, self.mouse_wheel_x, self.mouse_wheel_y = mouse_x, mouse_y, mouse_button, mouse_wheel_x or 0, mouse_wheel_y or 0;

    return self;
end

-- 滚动距离 需根据场景自行调节合适滚动大小
function Event:GetMouseWheelYDelta()
    if (self.mouse_wheel_y > 0) then 
        return -120;
    elseif (self.mouse_wheel_y == 0) then
        return 0;
    else 
        return 120;
    end
end

function Event:GetMouseWheelXDelta()
    if (self.mouse_wheel_x > 0) then 
        return -120;
    elseif (self.mouse_wheel_x == 0) then
        return 0;
    else 
        return 120;
    end
end


function Event:IsTripleClick()
    return self.quick_click_count == 3;
end

function Event:IsDoubleClick()
    return self.quick_click_count == 2;
end

function Event:GetXY()
    return self.mouse_x, self.mouse_y;
end

function Event:InitKeyEvent(window, key_event_type, keycode)
    self:Init(window, key_event_type);
    self.keycode = keycode;
    return self;
end

function Event:GetKeyCode()
    return self.keycode;
end

function Event:InitCharEvent(window, char)
    self:Init(window, "onchar");
    self.char = char;
    return self;
end

function Event:GetChar()
    return self.char;
end

Event:InitSingleton();

return Event;